[#include "/common/macro/header.ftl" /]
[@header title="个人资料" /]
<body>
<script type="text/html" template lay-url="/admin/users/info?edit=true" lay-done="layui.data.done(d);">
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">个人资料</div>
                    <div class="layui-card-body" pad15>
                        <form class="layui-form" action="" method="post">
                            <div class="layui-form-item">
                                <label class="layui-form-label">用户名</label>
                                <div class="layui-input-inline">
                                    {{d.data.username}}
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">当前密码</label>
                                <div class="layui-input-inline">
                                    <input type="password" lay-verify="oldPwd" lay-verType="tips"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">新密码</label>
                                <div class="layui-input-inline">
                                    <input type="password" lay-verify="password" lay-verType="tips"
                                           autocomplete="off" id="LAY-system-admin-form-profile-password"
                                           id="LAY_password" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">6个字符以上</div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">确认新密码</label>
                                <div class="layui-input-inline">
                                    <input type="password" name="pwd" lay-verify="password" lay-verType="tips"
                                           autocomplete="off" id="LAY-system-admin-form-profile-rePassword"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="name" placeholder="请输入名称" autocomplete="off"
                                           class="layui-input"
                                           lay-verify="required" maxlength="30" value="{{d.data.name}}">
                                </div>
                                <div class="layui-form-mid layui-word-aux">修改后重新登录生效</div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label">邮箱</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="email" placeholder="请输入邮箱" autocomplete="off"
                                           class="layui-input"
                                           lay-verify="email" maxlength="30" value="{{d.data.email}}">
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="updateCurrent">确认修改</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>
<script src="${base}/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${base}/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'admin', 'table'], function () {
        var $ = layui.$
                , admin = layui.admin
                , setter = layui.setter
                , layer = layui.layer
                , form = layui.form
                , response = setter.response;

        form.verify({
            password: function (value) {
                if (value) {
                    var reg = /^[^\s&\"<>]+$/;
                    if (!reg.test(value)) {
                        return '密码必须是英文或者数字！';
                    }
                    if (value.length < 6) {
                        return '密码最少6个字符';
                    }
                    if (value.length > 20) {
                        return '密码最多20个字符';
                    }
                    var pass = $("#LAY-system-admin-form-profile-password").val();
                    if (value != pass) {
                        return '两次输入的密码不一致！';
                    }
                    var repass = $("#LAY-system-admin-form-profile-rePassword").val();
                    if (value != repass) {
                        return '两次输入的密码不一致！';
                    }
                }
            },
            oldPwd: function (val) {
                if (val) {
                    var err;
                    admin.req({
                        url: '/admin/users/validate_pwd?pwd=' + val,
                        async: false,
                        success: function (res) {
                            err = res.error
                        },
                        error: function (err) {
                            console.error(err);
                            err = '服务器连接失败'
                        }
                    });
                    return err;
                }
            }
        });

        form.on('submit(updateCurrent)', function (data) {
            admin.req({
                url: '/admin/users'
                , type: 'put',
                contentType: "application/json"
                , data: JSON.stringify(data.field)
                , success: function (res) {
                    var statusCode = response.statusCode;
                    if (res[response.code] == statusCode.ok) {
                        layer.msg('修改个人信息成功', {
                            icon: 1,
                            time: 3000
                        }, function () {
                            if (top) {
                                top.layui.admin.closeThisTabs()
                            } else {
                                admin.closeThisTabs()
                            }
                        });
                    }
                }
            });
            return false;
        });

    });
    layui.data.done = function (d) {
    }
</script>
</body>
</html>
